package com.sky.web.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        //格式校验 非空大小不能为0 否者报错
        if (file == null || file.getSize() <= 0) {
            return Result.error("格式有误");
        }
        //给前端返回一个源文件名 文件输出流 得到一个路径
        String url = aliOssUtil.upload(file.getOriginalFilename(), file.getInputStream());
        //给前端返回路径
        return Result.success(url);

    }
}
